//
//  MYNewsRankVC.m
//  MuYing
//
//  Created by tbb on 2019/8/5.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYNewsRankVC.h"
#import "MYNewsRankCollViewCell.h"
#import "MYShopDetailsVC.h"
//#import "MYNewsCategoryModel.h"
#import "MYNewsRankHomeModel.h"
@interface MYNewsRankVC ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) QMUICollectionViewPagingLayout *collectionViewLayout;

@property (nonatomic, strong) UIPageControl * pageControl;
//@property (nonatomic, strong) NSMutableArray * categoryArr;

@property (nonatomic, strong) NSMutableDictionary * identifierDict;

@property (nonatomic, strong) MYNewsRankHomeModel * rankHomemModel;
@end

@implementation MYNewsRankVC

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *leftImage = YQIMG(@"nav_back_sIcon");
    [self.leftItem setImage:[leftImage qmui_imageWithTintColor:HexColor(0x79C4C7)] forState:(UIControlStateNormal)];
    self.navigationItem.title = kYQLS(@"排行榜");
    [self wr_setNavBarTitleColor:HexColor(0x79C4C7)];
//    self.rightItemImage = YQIMG(@"NewsRank_search_icon");
}

-(void)setupView {
    [super setupView];
    NSString *message=[NSString stringWithFormat:@"%@...",kYQLS(@"加载中")];
    YQSHOWLOADINGTIPS(message);
    ws(self);
    [YQPNetWorkingManager sendPostRequest:kPOST_RANKING_HOME_URL parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            self.rankHomemModel = [MYNewsRankHomeModel modelWithJSON:responseObject.data];
            weakself.pageControl.numberOfPages = self.rankHomemModel.rankingList.count;
            [weakself.collectionView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
    
}

#pragma mark --搜索
-(void)rightItemAction:(UIButton *)sender {
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (!CGSizeEqualToSize(self.collectionView.bounds.size, self.view.bounds.size)) {
        self.collectionView.frame = self.view.bounds;
        [self.collectionViewLayout invalidateLayout];
    }
    
    [self.view addSubview:self.pageControl];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-24);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(kYQPScreenWidth-72);
        make.centerX.mas_equalTo(0);
    }];
}
#pragma mark - <UICollectionViewDelegate, UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.rankHomemModel.catalogList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //不复用
    NSString *key = [NSString stringWithFormat:@"%zd",indexPath.row];
    NSString *identifier = self.identifierDict[key];
    if (identifier == nil) {
        identifier = [NSString stringWithFormat:@"cell%@",key];
        self.identifierDict[key] = identifier;
        [self.collectionView registerClass:[MYNewsRankCollViewCell class] forCellWithReuseIdentifier:identifier];
    }
    
    MYNewsRankCollViewCell *cell = (MYNewsRankCollViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    MYNewsRankRankingModel *rankingModel = nil;
    if (indexPath.row < self.rankHomemModel.rankingList.count) {
        rankingModel = self.rankHomemModel.rankingList[indexPath.row];
    }
    cell.rankingModel = rankingModel;
    cell.rankHomemModel = self.rankHomemModel;
    return cell;
}

#pragma mark - <UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {    
    CGSize size = CGSizeMake(CGRectGetWidth(collectionView.bounds) - UIEdgeInsetsGetHorizontalValue(self.collectionViewLayout.sectionInset) - UIEdgeInsetsGetHorizontalValue(self.collectionView.qmui_contentInset), CGRectGetHeight(collectionView.bounds) - UIEdgeInsetsGetVerticalValue(self.collectionViewLayout.sectionInset) - UIEdgeInsetsGetVerticalValue(self.collectionView.qmui_contentInset));
    
    return size;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat cx = scrollView.contentOffset.x;
    CGFloat cxMin = (scrollView.contentSize.width - kYQPScreenWidth) / self.rankHomemModel.catalogList.count;
    NSInteger index = cx / cxMin;
    self.pageControl.currentPage = index;
}

#pragma makr -- lazy
-(UICollectionView *) collectionView {
    if (!_collectionView) {
        QMUICollectionViewPagingLayout *collectionViewLayout = [[QMUICollectionViewPagingLayout alloc] initWithStyle:QMUICollectionViewPagingLayoutStyleScale];
        collectionViewLayout.minimumScale = 0.9;
        
        //collectionViewLayout.minimumInteritemSpacing = 0.001;
        //collectionViewLayout.minimumLineSpacing = ;
        
        
        self.collectionViewLayout = collectionViewLayout;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:collectionViewLayout];
        self.collectionView.backgroundColor = UIColorClear;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.view addSubview:self.collectionView];
        
        collectionViewLayout.sectionInset = UIEdgeInsetsMake(22, 36, 43, 36);
    }
    return _collectionView;
}

-(UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.pageIndicatorTintColor = HexColorA(0x000000, 0.12);
        _pageControl.currentPageIndicatorTintColor = kAPPThemeColor;
        
        _pageControl.currentPage = 0;
    }
    return _pageControl;
}

-(NSMutableDictionary *)identifierDict {
    if (!_identifierDict) {
        _identifierDict = [NSMutableDictionary dictionary];
    }
    return _identifierDict;
}
#pragma makr -- lazy
@end

